package com.currency.mall.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.currency.mall.Constant;
import com.currency.mall.R;
import com.currency.mall.event.MainEvent;
import com.currency.mall.netWork.NetWork;
import com.currency.mall.ui.login.LoginActivity;
import com.currency.mall.ui.web.WebViewActivity;
import com.mifengs.commonbase.base.BaseActivity;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.commonbase.util.StatusBarCompat;
import com.mifengs.commonbase.util.StatusBarUtils;
import com.mifengs.commonbase.util.ToastUitl;
import com.mifengs.commonbusines.entity.ResponseBean;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/25.
 */

public class SettingActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(getContext(), R.color.title_color));
        StatusBarUtils.StatusBarLightMode(this);
    }

    public void logout() {
        showLoading();
        NetWork.getLoginApi().logout()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        hideLoading();
                        if (responseBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            ToastUitl.show("退出成功", 200);
                            PreferenceUtils.clearLoginInfo();
                            EventBus.getDefault().post(new MainEvent(MainEvent.TAB_HOME));
                            finish();
                        } else if (responseBean.getCode().equals("9112")) {
                            toLogin();
                        } else {
                            ToastUitl.show(responseBean.getMsg(), 200);
                        }

                    }
                });
    }

    public void toLogin() {
        startActivity(new Intent(getContext(), LoginActivity.class));
    }

    @OnClick({R.id.ll_back, R.id.btn_exit, R.id.rl_update_pwd, R.id.rl_about})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.btn_exit:
                logout();
                break;
            case R.id.rl_update_pwd:
                startActivity(new Intent(getContext(), UpdatePwdActivity.class));
                break;
            case R.id.rl_about:
                startActivity(new Intent(getContext(), AboutActivity.class));
                break;
        }
    }
}
